//
//  UIImage+AvatarCutting.m
//  QXBSystem
//
//  Created by tongbinLi on 16/11/3.
//  Copyright © 2016年 tongbinLi. All rights reserved.
//

#import "UIImage+AvatarCutting.h"

@implementation UIImage (AvatarCutting)

-(UIImage *)zoom_4:(CGFloat)width{
    
    CGSize size = CGSizeMake(width/2, width/2);
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

-(UIImage *)zoom_2:(CGFloat)width{
    
    CGSize size = CGSizeMake(width, width);
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

-(UIImage *)left{
    
    UIImage * fooImage = self;
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width / 2.f;
    CGFloat _radius = radius;
    
    [path moveToPoint:CGPointMake(0, radius * 2)];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:_radius startAngle:M_PI_2 endAngle:M_PI clockwise:TRUE];
    
    [path moveToPoint:CGPointMake(0, 0)];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:_radius startAngle:M_PI endAngle:1.5 * M_PI clockwise:TRUE];

    [path moveToPoint:CGPointMake(radius - 1, 0)];
    [path addLineToPoint:CGPointMake(radius - 1, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, 0)];
    
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    
    [path addClip];
    [fooImage drawInRect:imageRect];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

-(UIImage *)right{
    
    UIImage * fooImage = self;
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width / 2.f;
    CGFloat _radius = radius;
    [path moveToPoint:CGPointMake(radius, 0)];
    [path addArcWithCenter:CGPointMake(0, radius) radius:_radius startAngle: 1.5 * M_PI endAngle:0 clockwise:TRUE];
    
    [path moveToPoint:CGPointMake(radius, radius * 2)];
    [path addArcWithCenter:CGPointMake(0, radius) radius:_radius startAngle:0 endAngle:M_PI_2 clockwise:TRUE];
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path addClip];
    [fooImage drawInRect:imageRect];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}
- (UIImage *)leftTop
{
    UIImage * fooImage = self;
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width;
    CGFloat _radius = radius;
    [path moveToPoint:CGPointMake(0, 0)];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:_radius startAngle:M_PI endAngle:1.5 * M_PI clockwise:TRUE];
    
    [path moveToPoint:CGPointMake(fooImage.size.width - 1, 0)];
    [path addLineToPoint:CGPointMake(fooImage.size.width - 1, fooImage.size.height - 1)];
    [path addLineToPoint:CGPointMake(0, fooImage.size.height - 1)];
    [path addLineToPoint:CGPointMake(0, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, 0)];
    
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path addClip];
    [fooImage drawInRect:imageRect];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

-(UIImage *)leftBottom{
    
    UIImage * fooImage = self;
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width;
    CGFloat _radius = radius;
    [path moveToPoint:CGPointMake(0, fooImage.size.height)];
    [path addArcWithCenter:CGPointMake(radius, fooImage.size.height - radius) radius:_radius startAngle:M_PI_2 endAngle:M_PI clockwise:TRUE];
    
    [path moveToPoint:CGPointMake(fooImage.size.width - 1, 0)];
    [path addLineToPoint:CGPointMake(fooImage.size.width - 1, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, 0)];
    
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path addClip];
    [fooImage drawInRect:imageRect];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

-(UIImage *)rightTop{
    
    UIImage * fooImage = self;
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width;
    CGFloat _radius = radius;
    [path moveToPoint:CGPointMake(fooImage.size.width, 0)];
    [path addArcWithCenter:CGPointMake(0, fooImage.size.height) radius:_radius startAngle:1.5 *M_PI endAngle:0 clockwise:TRUE];
    
    [path moveToPoint:CGPointMake(0, fooImage.size.height - 1)];
    [path addLineToPoint:CGPointMake(0, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, fooImage.size.height)];
    [path addLineToPoint:CGPointMake(fooImage.size.width, fooImage.size.height - 1)];
    
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path addClip];
    [fooImage drawInRect:imageRect];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

-(UIImage *)rightBottom{
    
    UIImage * fooImage = self;
    CGRect imageRect = CGRectMake(0, 0, fooImage.size.width, fooImage.size.height);
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:imageRect];
    CGFloat radius = fooImage.size.width;
    CGFloat _radius = radius;
    [path moveToPoint:CGPointMake(fooImage.size.width, fooImage.size.height)];
    [path addArcWithCenter:CGPointMake(0, 0) radius:_radius startAngle:0 endAngle:M_PI_2 clockwise:TRUE];
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path addClip];
    [fooImage drawInRect:imageRect];
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return maskedImage;
}

+(UIImage *)synthesis:(CGSize )size imgs:(NSArray<__kindof UIImage *> *)imgArray{
    
    if (imgArray.count >= 2 && imgArray.count <= 4) {
        
        UIGraphicsBeginImageContext(size);
        int i = 0;
        for (UIImage *img in imgArray) {
        
            CGPoint point = CGPointZero;
            if (i == 0) {
                
                point = CGPointMake(0, 0);
            }
            else if (i == 1){
                
                point = CGPointMake(size.width / 2.f, 0);
            }
            else if (i == 2){
                
                if (imgArray.count == 3) {
                    
                    point = CGPointMake(size.width / 2.f, size.height / 2.f);
                }
                else{
                    
                    point = CGPointMake(0, size.height / 2.f);
                }
            }
            else if (i == 3){
                
                point = CGPointMake(size.width / 2.f, size.height / 2.f);
            }
            [img drawInRect:CGRectMake(point.x,point.y,img.size.width,img.size.height)];
            i++;
        }
        UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resultingImage;
    }
    return nil;
}
@end
